<template>
  <div class="component-upload-image">
    <el-upload
      :action="uploadImgUrl"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      name="file"
      :show-file-list="false"
      :headers="headers"
      :accept="accept"
      style="display: inline-block; vertical-align: top"
    >
      <div v-if="type == 1">
        <my-image v-if="!value" :src="value">
          <div slot="error" class="image-slot">
            <i class="el-icon-plus" />
          </div>
        </my-image>
        <div v-else class="image">
          <my-image
            :src="value"
            fit="fill"
            :style="`width:98px;height:98px;`"
          />
          <div class="mask">
            <div class="actions">
              <span title="预览" @click.stop="dialogVisible = true">
                <i class="el-icon-zoom-in" />
              </span>
              <span title="移除" @click.stop="removeImage">
                <i class="el-icon-delete" />
              </span>
            </div>
          </div>
        </div>
      </div>
      <div v-if="type == 2" style="width:100%;height:100%;">
        <my-video v-if="!value" :src="value">
          <div slot="error" class="image-slot">
            <i class="el-icon-plus" />
          </div>
        </my-video>
        <div v-else class="image">
          <my-video
            :src="value"
            fit="fill"
          />
          <div class="mask">
            <div class="actions">
              <span title="预览" @click.stop="dialogVisible = true">
                <i class="el-icon-zoom-in" />
              </span>
              <span title="移除" @click.stop="removeImage">
                <i class="el-icon-delete" />
              </span>
            </div>
          </div>
        </div>
      </div>
    </el-upload>
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <my-image
        v-if="type==1"
        :src="value"
        style="display: block; max-width: 100%; margin: 0 auto;"
      />
      <my-video
        v-if="type==2"
        :src="value"
        controls="controls"
        style="display: block; max-width: 100%; margin: 0 auto;"
      />
    </el-dialog>

  </div>
</template>

<script>
import { getToken } from '@/utils/auth'

export default {
  props: {
    value: {
      type: String,
      default: ''
    },
    accept: {
      type: String,
      default: 'image/*'
    },
    type: {
      type: String,
      default: '1' // 1图 2视频
    }
  },
  data() {
    return {
      dialogVisible: false,
      uploadImgUrl: process.env.VUE_APP_BASE_API + '/cms/common/upload', // 上传的图片服务器地址
      headers: {
        Authorization: 'Bearer ' + getToken()
      }
    }
  },
  // computed: {
  //   valueSrc() {
  //     return process.env.VUE_APP_IMG_HOST + this.value
  //   }
  // },
  methods: {
    removeImage() {
      this.$emit('input', '')
    },
    handleUploadSuccess(res) {
      this.$emit('input', res.data.fileName)
      this.loading.close()
    },
    handleBeforeUpload() {
      this.loading = this.$loading({
        lock: true,
        text: '上传中',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    },
    handleUploadError() {
      this.$message({
        type: 'error',
        message: '上传失败'
      })
      this.loading.close()
    }
  }
}
</script>

<style scoped lang="scss">
.image {
  position: relative;
    width: 100%;
    height: 100%;
  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
  }
  &:hover .mask {
    opacity: 1;
  }
}
</style>
